<?php

/**
 * make actions.
 *
 * @package    MVM Vehicle Showroom (r) showroom
 * @subpackage make
 * @author     Bo Wang
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class stocklistActions extends sfActions {

  /**
   * Executes index action
   *
   * @param sfRequest $request A request object
   */
  public function executeIndex(sfWebRequest $request) {
    $make_id = $request->getParameter('make_id',null);
    $model_id = $request->getParameter('model_id',null);
    $type = $request->getParameter('type',null);
    $body_style_id = $request->getParameter('body_style_id',null);
    $price_range_id = $request->getParameter('price_range_id',null);

    $this->params='';
    $this->pricerange = VehicleTable::$conts['vehicle_price_range'];
    $this->bodystyle = Doctrine::getTable('BodyStyle')->findAll();

    $this->vehicles_query = Doctrine::getTable('Vehicle')
            ->createQuery('v');

    $urlparams = array();

    if($make_id) {
      $this->vehicles_query->where('v.make_id = ?', $make_id);
      $urlparams['make_id']=$make_id;
      $this->title = MakeTable::getInstance()->find($make_id)->getName();
    }
    if($model_id) {
      $this->vehicles_query->andWhere('v.model_id = ?', $model_id);
      $urlparams['model_id']=$model_id;
      $this->title = ModelTable::getInstance()->find($model_id)->getName();
    }
    if($type=='specials') {
      $this->vehicles_query->andWhere('v.is_special = ?', '1');
      $urlparams['type']='specials';
      $this->title = 'Vehicles on Special';
    }
    if($body_style_id) {
      $this->vehicles_query->andWhere('v.body_style_id = ?', $body_style_id);
      $flag = true;
      $this->title = BodyStyleTable::getInstance()->find($body_style_id)->getName();
    }
    if($price_range_id!==null) {
      $this->vehicles_query->andWhere('v.price '. $this->pricerange[$price_range_id]['value']);
    }
    if(count($urlparams)) {
      $this->params= http_build_query($urlparams).'&';
    }

    if($this->vehicles_query->count()<1) return sfView::ERROR;

    $this->vehicles_query->addOrderBy('v.created_at DESC');

    $this->pager = new sfDoctrinePager(
            'StockList',
            sfConfig::get('app_max_vehicle_on_list')
    );

    $this->pager->setQuery($this->vehicles_query);
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();

    if(!isset($this->title)) {
      $this->title='Stock List';
    }
    $this->getResponse()->setTitle($this->title.' @ '. sfConfig::get('app_company_name'));
    
  }

}
